<template>
  <view class="container">
    <z-paging-swiper>
      <view slot="top">
        <u-navbar
          height="50"
          :border-bottom="false"
          title="交易订单"
          title-size="36"
          title-color="#333"
        ></u-navbar>
        <view class="tabsView">
          <u-tabs-swiper
            ref="uTabs"
            :list="tabsList"
            :current="current"
            @change="tabsChange"
            :is-scroll="false"
            :show-bar="false"
            active-color="#10A28F"
            swiper-width="750"
          >
          </u-tabs-swiper>
        </view>
        <u-gap height="20" bg-color="#F0F1F2"></u-gap>
      </view>
      <swiper
        class="swiperView"
        :current="scurrent"
        @transition="transition"
        @animationfinish="animationfinish"
      >
        <swiper-item v-for="(_sitem, sindex) in tabsList" class="swiperItem" :key="sindex">
          <z-paging
            ref="paging"
            v-model="itemList[sindex]"
            @query="queryList"
            :fixed="true"
            :auto="false"
            :auto-show-back-to-top="true"
            :empty-view-img="`${getImageUrl('empty.png')} `"
            empty-view-text="暂无订单数据~"
            :empty-view-img-style="imgStyle"
          >
            <view class="goodsItemView">
              <view
                class="itemView"
                v-for="(item, index1) in itemList[sindex]"
                :key="index1"
                @click="onOrderDetail(item)"
              >
                <view class="order-top dis-flex flex-y-center flex-y-between">
                  <text class="dis-flex flex-y-center">单号:{{ item.no }}</text>
                  <text class="order-top-tip">
                    <text
                      v-if="item.type === 10"
                      style="font-weight: bold; font-size: 28rpx; margin-right: 20rpx"
                      >外卖
                    </text>
                    <text
                      v-if="item.reserveDate"
                      style="
                        color: #cb6501;
                        font-weight: bold;
                        font-size: 28rpx;
                        margin-right: 20rpx;
                      "
                      >预约</text
                    >
                    <text v-if="item.status === 10 || item.status === 20" style="color: orange">{{
                      item.statusName
                    }}</text>
                    <text v-else>{{ item.statusName }}</text>
                  </text>
                </view>
                <view class="layoutView">
                  <view class="order-goods" v-for="item2 in item.items" :key="item2.id">
                    <u-image
                      class="goods-img"
                      :src="item2.picUrl"
                      width="160rpx"
                      height="160rpx"
                      border-radius="10"
                    ></u-image>
                    <view class="orde-riget" style="flex: 1">
                      <view class="goods-title">
                        <text>{{ item2.spuName }}</text>
                      </view>
                      <view class="goods-speci dis-flex flex-x-baseline">
                        <text v-if="item2.properties && item2.properties.length > 0"
                          >{{ item2.properties[0].valueName }}
                        </text>
                        <text class="goodsNum" style="margin-left: 20rpx"
                          >{{ item2.count }}{{ item2.unitValue > 1 ? 'x' + item2.unitValue : ''
                          }}{{ item2.unitName || '' }}
                        </text>
                        <view class="flex-1"></view>
                        <text class="goodsNum" v-if="item2.weight"
                          >约{{ (item2.count * item2.weight * 1000).toFixed(0) }}克
                        </text>
                      </view>
                      <view class="goods-price" v-if="item2.afterSaleId">
                        <text>¥{{ (item2.price * 0.01).toFixed(2) }}</text>
                        <text
                          class="goods-refund"
                          v-if="
                            item2.afterSaleStatus === 10 ||
                            item2.afterSaleStatus === 20 ||
                            item2.afterSaleStatus === 30
                          "
                        >
                          退款进行中
                        </text>
                        <text class="goods-refund" v-else-if="item2.afterSaleStatus === 40">
                          等待平台退款
                        </text>
                        <text class="goods-refund" v-else-if="item2.afterSaleStatus === 50">
                          退款成功
                        </text>
                        <text class="goods-refund" v-else> 已终止退款</text>
                      </view>
                    </view>
                  </view>
                </view>
                <view class="price-total dis-flex flex-y-center flex-y-between">
                  <view>共{{ item.items.length }}样</view>
                  <view>
                    合计：
                    <text style="color: #fa453c">¥{{ (item.payPrice * 0.01).toFixed(2) }}</text>
                  </view>
                </view>
                <view
                  class="btn-group"
                  v-if="item.status == 10 && item.deliveryType == 1 && item.refundStatus === 0"
                >
                  <view class="solid-btn btn" @click.stop.prevent="onOrderDetail(item)">
                    去发货
                  </view>
                </view>
                <view
                  class="btn-group"
                  v-else-if="
                    item.status == 10 &&
                    (item.deliveryType == 2 || item.deliveryType == 3 || item.deliveryType == 4) &&
                    item.refundStatus === 0
                  "
                >
                  <view
                    v-if="item.dineStatus == 10"
                    class="solid-btn btn"
                    @click.stop.prevent="onOrderDetail(item)"
                  >
                    去接单</view
                  >
                  <view
                    v-else-if="item.dineStatus == 20 || item.dineStatus == 22"
                    class="solid-btn btn"
                    @click.stop.prevent="onOrderDetail(item)"
                  >
                    去出餐</view
                  >
                  <view class="btn-group" v-else>
                    <view @click.stop.prevent="onOrderDetail(item)" class="hollow-btn btn">
                      查看详情
                    </view>
                  </view>
                </view>
                <view
                  class="btn-group"
                  v-else-if="(item.status == 20 || item.status == 30) && item.deliveryType == 1"
                >
                  <view @click.stop.prevent="onExpress(item)" class="hollow-btn btn">
                    查看物流
                  </view>
                </view>
                <view
                  class="btn-group"
                  v-else-if="
                    (item.status == 10 || item.status == 20) &&
                    (item.deliveryType == 2 || item.deliveryType == 4)
                  "
                >
                  <view @click.stop.prevent="onClerk()" class="solid-btn btn"> 去核销</view>
                </view>
                <view class="btn-group" v-else>
                  <view @click.stop.prevent="onOrderDetail(item)" class="hollow-btn btn">
                    查看详情
                  </view>
                </view>
              </view>
            </view>
          </z-paging>
        </swiper-item>
      </swiper>
    </z-paging-swiper>
  </view>
</template>

<script>
export default {
  data() {
    return {
      imgStyle: {
        width: '12rem'
      },
      current: 0,
      scurrent: 0,
      tabsList: [
        {
          name: '全部',
          status: ''
        },
        {
          name: '待发货/待配送',
          status: 10
        },
        {
          name: '待收货/待核销',
          status: 20
        },
        {
          name: '已完成',
          status: 30
        }
      ],
      itemList: [[], [], [], []],
      order_id: ''
    }
  },
  onLoad(res) {
    if (res.index) {
      this.current = Number(res.index) ? Number(res.index) : 0
      this.scurrent = Number(res.index) ? Number(res.index) : 0
    }
    if (res.status) {
      let _index = this.tabsList.findIndex((item) => item.status == res.status)
      console.log(_index, res.status)
      this.current = _index ? _index : 0
      this.scurrent = _index ? _index : 0
    }
    this.$u.get('/app-api/trade/delivery/express/list', {}).then((res) => {
      this.exprees = res.data
    })
  },
  onShow() {
    this.queryList()
  },
  methods: {
    tabsChange(index) {
      this.scurrent = index
    },
    transition(e) {
      let dx = e.detail.dx
      this.$refs.uTabs.setDx(dx)
    },
    animationfinish(e) {
      this.current = e.detail.current
      this.scurrent = e.detail.current
      this.$refs.uTabs.setFinishCurrent(e.detail.current)
      if (this.itemList[this.current].length == 0) {
        this.queryList(1)
      }
    },
    queryList(pageNo = 1, pageSize = 10) {
      this.$u
        .get('/app-api/trade/order/page-seller', {
          pageNo,
          pageSize,
          status: this.tabsList[this.scurrent].status
        })
        .then((res) => {
          this.$refs.paging[this.scurrent].complete(res.data.list)
        })
        .catch(() => {
          this.$refs.paging[this.scurrent].complete(false)
        })
    },
    onOrderDetail(item) {
      this.$utils.route({
        url: '/store/order-details/order-details',
        params: {
          id: item.id
        }
      })
    },
    onExpress(e) {
      uni.navigateTo({
        url:
          '/other/express-process/express-process?id=' +
          e.id +
          '&goodsJson=' +
          JSON.stringify(e.items[0])
      })
    },
    onClerk() {
      this.$utils.route('/store/scan')
    }
  }
}
</script>

<style lang="scss" scoped>
@import url('order-list.css');
@import url('@/style/btn.css');

.slot-btn {
  width: 200rpx;
  height: 200rpx;
  background: #f5f5f5;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-flow: column;
}
</style>
